<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

/**
 * App\CourseStudyLenght
 *
 * @property int $id
 * @property int $user_id 用户id
 * @property int $course_id 课程id
 * @property float $cumulative_time 累计学习时长
 * @property string|null $deleted_at
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @method static \Illuminate\Database\Eloquent\Builder|\App\CourseStudyLenght newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\CourseStudyLenght newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\CourseStudyLenght query()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\CourseStudyLenght whereCourseId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\CourseStudyLenght whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\CourseStudyLenght whereCumulativeTime($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\CourseStudyLenght whereDeletedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\CourseStudyLenght whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\CourseStudyLenght whereUpdatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\CourseStudyLenght whereUserId($value)
 * @mixin \Eloquent
 * @method static bool|null forceDelete()
 * @method static \Illuminate\Database\Query\Builder|\App\CourseStudyLenght onlyTrashed()
 * @method static bool|null restore()
 * @method static \Illuminate\Database\Query\Builder|\App\CourseStudyLenght withTrashed()
 * @method static \Illuminate\Database\Query\Builder|\App\CourseStudyLenght withoutTrashed()
 */
class CourseStudyLenght extends Model
{
    use SoftDeletes;
    //
    public static function getUserStudyCourseLength($user_id,$course_id){
        if(empty($user_id) || empty($course_id))
            return 0;

        $courseStudyLength = self::whereCourseId($course_id)->whereUserId($user_id)->first();
        if(!$courseStudyLength)
            return 0;

        $timeLength = 0;
        if((int)$courseStudyLength->cumulative_time > 0){
            $timeLength = gmdate("H:i:s",(int)$courseStudyLength->cumulative_time);
        }

        return $timeLength;
    }
}
